#!/usr/bin/env perl
use warnings;
use strict;

# Run BellKor kNN for various similarity files.

# Precomputed files (see definitions in Makefile).
my $query = "probe_by_user.txt";
my $Ahat = "bellkor_Ahat.bin";

# Names of knn prediction tuple files; updated by knn methods.
my @pred_tuples = ();

sub bellkor_knn {
  my ($ts, $similarity_bin, $k) = @_; 
  $similarity_bin =~ /(.*)\.similarity\.bin/ or die "bad: $similarity_bin";
  my $out = "$1_bk_knn_$k.$ts.pred.residuals.tuples";
  my $err = "$out.err";
  push @pred_tuples, $out;
print <<STR;
$out: $ts $query $similarity_bin $Ahat
\tbin/bellkor_knn $query $similarity_bin $k <$ts 2>$err | \\
\tbin/bellkor_knn_pred $Ahat >$out 2>>$err
STR
}

#my @sim_files;
#push @sim_files, glob("*.similarity.bin");
my @sim_files = (
  "bellkor_pearson.similarity.bin",
  );
my @ks = (20, 50);
my @tss = (
  "user_residuals.txt",
  );

for my $ts (@tss) {
  for my $sim (@sim_files) {
    for my $k (@ks) {
      bellkor_knn($ts, $sim, $k);
    }
  }
}

# Rules to make all knn results.
my @preds = @pred_tuples;
map {s/\.residuals\.tuples/.res/} @preds;
my $targets = join(" ", @preds);
print <<EOF;
knn_res: $targets
EOF

# See what happens when we use the smoothed training set from the start.
my @knn_full_tuples = ();
sub bk_full_knn {
  my ($similarity_bin, $ts_by_user, $beta, @ks) = @_; 
  $similarity_bin =~ /(.*)\.similarity\.bin/ or die "bad: $similarity_bin";
  my $stem = "$1_bk_full_knn_$ts_by_user";
  my $ts_by_movie = "$ts_by_user.by_movie";
  my $Abar = "$stem.Abar.bin";
  my $U = "$stem.U.bin";
  my $Ahat = "$stem.$beta.Ahat.bin";

  print <<STR;
$Abar: $ts_by_movie
\tbin/bellkor_Abar $Abar $U <$ts_by_movie 2>$Abar.err
$Ahat: $Abar
\tbin/bellkor_Ahat $Abar $U $beta >$Ahat 2>$Ahat.err
STR

  for my $k (@ks) {
    my $out = "$stem"."_$k"."_$beta.pred.residuals.tuples";
    push @knn_full_tuples, $out;
    print <<STR;
$out: $ts_by_user $similarity_bin $Ahat $Abar
\tbin/bellkor_knn $query $similarity_bin $k <$ts_by_user 2>$out.err | \\
\tbin/bellkor_knn_pred $Ahat >$out 2>>$out.err
STR
  }
}

bk_full_knn("bellkor_pearson.similarity.bin",
  "smooth_users_bellkor_pearson.similarity.bin.50.txt",
  500, 50);
bk_full_knn("bellkor_pearson.similarity.bin",
  "smooth_movies_bellkor_pearson.similarity.bin.100.txt",
  500, 50);
bk_full_knn("link_noredir_0.new.similarity.bin",
  "smooth_users_link_noredir_0.new.similarity.bin.50.txt",
  500, 50);
bk_full_knn("link_noredir_0.new.similarity.bin",
  "smooth_movies_link_noredir_0.new.similarity.bin.200.txt",
  500, 50);

# Rules to make all knn results.
my @knn_full_preds = @knn_full_tuples;
map {s/\.residuals\.tuples/.res/} @knn_full_preds;
my $full_targets = join(" ", @knn_full_preds);
print <<EOF;
full_knn_res: $full_targets
EOF

# Copyright (c) 2009 John Lees-Miller
# 
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

